package lily.phs.entertain;

import java.util.ArrayList;
import java.util.HashMap;

import lily.phs.urh.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class RecentlyActivity extends Activity {
	private DBHelper dbHelper = null;
	private ListView listview;
	private int[] _ids;
	private String[]_titles;
	Cursor cursor = null;
	int[] music_id;
	TextView tv_TitleBar;
//	private GridView toolbarGrid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		this.setContentView(R.layout.media_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
		
		tv_TitleBar = (TextView)this.findViewById(R.id.title);
        tv_TitleBar.setText("最近播放音乐");
        Button btBack = (Button)this.findViewById(R.id.title_left_btn);
		btBack.setVisibility(View.GONE);
		Button btSave = (Button)this.findViewById(R.id.title_right_btn);
		btSave.setVisibility(View.GONE);
		Button btSubmit = (Button)this.findViewById(R.id.title_right_btn2);
		btSubmit.setVisibility(View.GONE);
		
        listview = (ListView)findViewById(R.id.media_main_List);
        
		dbHelper = new DBHelper(this, "music.db", null, 2);
		cursor = dbHelper.queryRecently();
		cursor.moveToFirst();
		int num;
		if (cursor!=null){
			num = cursor.getCount();
		} else{
			return;
		}
		String idString ="";
		if (num>=10){
			for(int i=0;i<10;i++){
				music_id = new int[10];
				music_id[i]=cursor.getInt(cursor.getColumnIndex("music_id"));
				if (i<9){
					idString = idString+music_id[i]+",";
				} else{
					idString = idString+music_id[i];
				}
				cursor.moveToNext();
			} 
		}else if(num>0){
			for(int i=0;i<num;i++){
				music_id = new int[num];
				music_id[i]=cursor.getInt(cursor.getColumnIndex("music_id"));
				if (i<num-1){
					idString = idString+music_id[i]+",";
				} else{
					idString = idString+music_id[i];
				}
				cursor.moveToNext();
			}
		}
		if (cursor!=null){
			cursor.close();
			cursor=null;
		}
		System.out.println(idString);
		listview = new ListView(this);
		Cursor c = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        			new String[]{MediaStore.Audio.Media.TITLE,
					MediaStore.Audio.Media.DURATION,
					MediaStore.Audio.Media.ARTIST,
					MediaStore.Audio.Media._ID,
					MediaStore.Audio.Media.DISPLAY_NAME} , MediaStore.Audio.Media._ID + " in ("+ idString + ")", null,null);
		
		  c.moveToFirst();
	        _ids = new int[c.getCount()];
	        _titles = new String[c.getCount()];
	        for(int i=0;i<c.getCount();i++){
	        	_ids[i] = c.getInt(3);
	        	_titles[i] = c.getString(0);
	        	
	        	c.moveToNext();
	        }
	        listview.setAdapter(new MusicListAdapter(this, c));
	        listview.setOnItemClickListener(new ListItemClickListener());
	        //setContentView(listview);
	       
	    }
	   
	    class ListItemClickListener implements OnItemClickListener{

	    	@Override
	    	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
	    		// TODO Auto-generated method stub
	    		Intent intent = new Intent(RecentlyActivity.this,MusicActivity.class);
	    		intent.putExtra("_ids", _ids);
	    		intent.putExtra("_titles", _titles);
	    		intent.putExtra("position", position);
	    		startActivity(intent);
	    		finish();
	    	}
	    	
	    }
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == event.KEYCODE_BACK) {
				Intent intent = new Intent();
				intent.setClass(this, MediaMainActivity.class);
				startActivity(intent);
				finish();
			}
			return true;
	    }
	    
	    
}
